package simulators;

import java.util.Vector;
import types.TextMessage;

public class MessageInbox
{
    public static Object myLock = new Object();
    /** 
     * For test purposes, a Vector will act as the inbox.  
     */
    private volatile static Vector<TextMessage> messageInbox = new Vector<TextMessage>();
    
    /**
     * Thread safe message getter
     * @return
     */
    public static TextMessage getMessage()
    {
	synchronized(myLock)
	{
	    if(messageInbox.isEmpty())
		{
		    return null;
		}
		else
		{
		    TextMessage tm = messageInbox.get(0);
		    messageInbox.remove(0);
		    return tm;
		}
	}
    }
    
    /**
     * Thread safe message adder
     * @param tm
     */
    public static void addMessage(TextMessage tm)
    {
	synchronized(myLock)
	{
	    messageInbox.add(tm);
	}
    }
}
